package com.youkeyi.ddy.cloud.basic.crm.laboratory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.crm.laboratory.entity.TblLaboratory;
import com.youkeyi.ddy.cloud.basic.crm.laboratory.service.TblLaboratoryService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.laboratory.CrmLaboratoryCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.laboratory.dto.CrmLaboratoryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.laboratory.dto.CrmLaboratoryListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.laboratory.param.CrmLaboratoryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.laboratory.param.CrmLaboratoryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.laboratory.param.CrmLaboratoryQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.laboratory.param.CrmLaboratoryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 化验所表 前端控制器
 * </p>
 *
 * @author xiawang.liu
 * @since 2024-03-12
 */
@Service
public class TblLaboratoryController implements CrmLaboratoryCloudService {

    @Autowired
    private TblLaboratoryService tblLaboratoryService;

    @Override
    public CrmLaboratoryListCloudDto query(CrmLaboratoryQueryCloudParam param) {
        CrmLaboratoryListCloudDto dto = new CrmLaboratoryListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getLaboratoryPrimaryKeys())) {
            param.getLaboratoryPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCodes())) {
            param.getCodes().removeAll(Collections.singleton(null));
        }

        QueryWrapper<TblLaboratory> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getLaboratoryPrimaryKeys()), TblLaboratory::getPrimaryKey, param.getLaboratoryPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), TblLaboratory::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCodes()), TblLaboratory::getCode, param.getCodes())
                .like(StringUtils.isNotBlank(param.getNameFuzzy()), TblLaboratory::getName, param.getNameFuzzy())
                .like(StringUtils.isNotBlank(param.getContactsFuzzy()), TblLaboratory::getContacts, param.getContactsFuzzy())
                .ne(TblLaboratory::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<TblLaboratory> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<TblLaboratory> laboratoryList = tblLaboratoryService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(laboratoryList.getRecords())) {
            dto.setTotal(laboratoryList.getTotal());

            List<CrmLaboratoryListItemCloudDto> items = new ArrayList<>();
            laboratoryList.getRecords().forEach(laboratory -> {
                CrmLaboratoryListItemCloudDto item = new CrmLaboratoryListItemCloudDto();
                item.setLaboratoryPrimaryKey(laboratory.getPrimaryKey());
                item.setCorpKey(laboratory.getCorpKey());
                item.setCode(laboratory.getCode());
                item.setName(laboratory.getName());
                item.setPhone(laboratory.getPhone());
                item.setLetterEncoding(laboratory.getLetterEncoding());
                item.setContacts(laboratory.getContacts());
                item.setAddress(laboratory.getAddress());
                item.setDesc(laboratory.getDesc());
                item.setRemark(laboratory.getRemark());
                item.setIsDeleted(laboratory.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(laboratory.getCreateTime() != null ? laboratory.getCreateTime().getTime() : null);
                item.setCreateUserKey(laboratory.getCreateUserKey());
                item.setCreateUserName(laboratory.getCreateUserName());
                item.setUpdateTime(laboratory.getUpdateTime() != null ? laboratory.getUpdateTime().getTime() : null);
                item.setUpdateUserKey(laboratory.getUpdateUserKey());
                item.setUpdateUserName(laboratory.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(CrmLaboratoryCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param
                || StringUtils.isBlank(param.getCorpKey()) || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        TblLaboratory laboratory = new TblLaboratory();
        laboratory.setPrimaryKey(SimpleUUidUtils.getUuid());
        laboratory.setCorpKey(param.getCorpKey());
        laboratory.setCode(param.getCode());
        laboratory.setName(param.getName());
        laboratory.setPhone(param.getPhone());
        laboratory.setAddress(param.getAddress());
        laboratory.setLetterEncoding(param.getLetterEncoding());
        laboratory.setContacts(param.getContacts());
        laboratory.setDesc(param.getDesc());
        laboratory.setRemark(param.getRemark());
        laboratory.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        laboratory.setCreateUserKey(param.getCreateUserKey());
        laboratory.setCreateUserName(param.getCreateUserName());
        laboratory.setCreateTime(new Date());
        boolean save = tblLaboratoryService.save(laboratory);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(laboratory.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent update(CrmLaboratoryUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getLaboratoryPrimaryKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        UpdateWrapper<TblLaboratory> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(TblLaboratory::getPrimaryKey, param.getLaboratoryPrimaryKey());
        TblLaboratory laboratory = new TblLaboratory();
        laboratory.setPrimaryKey(param.getLaboratoryPrimaryKey());
        laboratory.setCode(param.getCode());
        laboratory.setName(param.getName());
        laboratory.setPhone(param.getPhone());
        laboratory.setLetterEncoding(param.getLetterEncoding());
        laboratory.setContacts(param.getContacts());
        laboratory.setAddress(param.getAddress());
        laboratory.setDesc(param.getDesc());
        laboratory.setRemark(param.getRemark());
        laboratory.setUpdateUserKey(param.getUpdateUserKey());
        laboratory.setUpdateUserName(param.getUpdateUserName());
        laboratory.setUpdateTime(new Date());
        boolean save = tblLaboratoryService.update(laboratory, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        return rc;
    }

    @Override
    public ResponseContent delete(CrmLaboratoryDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getLaboratoryPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<TblLaboratory> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(TblLaboratory::getPrimaryKey, param.getLaboratoryPrimaryKeys());

        TblLaboratory laboratory = new TblLaboratory();
        laboratory.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        laboratory.setUpdateUserKey(param.getUpdateUserKey());
        laboratory.setUpdateUserName(param.getUpdateUserName());
        laboratory.setUpdateTime(new Date());
        boolean save = tblLaboratoryService.update(laboratory, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
